//
//  NLTabBar.swift
//  sinaWeibo7110
//
//  Created by 安静清晨 on 2017/1/4.
//  Copyright © 2017年 walk-in-minds. All rights reserved.
//

import UIKit

class NLTabBar: UITabBar {

    //声明一个闭包
    //MARK: flag
    var btnClosure:(() -> ())?
    
    
    var btn = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.setupUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //➕按钮
    func setupUI(){
        
        let btn = UIButton()
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: UIControlState.normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        addSubview(btn)
        
        btn.sizeToFit()
        self.btn = btn
        
        btn.addTarget(self, action: #selector(didClick), for: .touchUpInside)
        
    }
    
    
    // 按钮点击事件
    func didClick(){
        
        btnClosure?()
        
    }
    
    
    
    
    
    override func layoutSubviews() {
      
        super.layoutSubviews()
        
        let width = bounds.width / 5
        let height = bounds.height
        
        self.btn.frame = CGRect(x: CGFloat(2) * width, y: 0, width: width, height: height)
        
        //遍历子控件.抓取其中的tabBarItem.
        
        //设置一个增量
        var index = 0
        
        for subView in subviews{
            //MARK: UITabBarButton是私有类. 所以不能直接用UITabBarButton.self来表示类
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: height)
                
//                index == 1 ? index +=2 : index +=1
                index += (index == 1 ? 2 : 1)
            }
            
            
        }
        
        
        
    }
    
    

}
